package com.cmc.cloud.cmclink.doc.controller.admin.multimodaltransport;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.cmes.framework.operatelog.core.annotations.OperateLog;import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cmc.cloud.cmclink.doc.api.document.emums.ExImTypeEnum;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.service.multimodaltransport.ContainerManifestService;
import com.cmc.cloud.cmclink.doc.util.FileOperateUtil;
import com.cmc.cloud.cmclink.doc.vo.multimodaltransport.containermanifest.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 落驳集装箱清单-进口
 *
 * @author dev89
 * @date 2025/10/17 13:16
 */
@Tag(description = "后台管理-落驳集装箱清单-进口", name = "后台管理-落驳集装箱清单-进口")
@RestController
@RequestMapping(UrlConstants.APPLICATION_URL + "/containerManifest/IM")
public class ContainerManifestImController {

    @Resource
    private ContainerManifestService containerManifestService;

    @OperateLog(type = OperateTypeEnum.IMPORT)
    @Operation(summary = "导入落驳集装箱清单-CNNGB")
    @PostMapping("/import/CNNGB")
    @PreAuthorize("@ss.hasPermission('doc:offloadinglist:import')")
    public CommonResult<Void> importContainerManifestCnngb(
        @Valid @RequestBody ContainerManifestImportAllReqVO containerManifestImportAllReqVO) {
        containerManifestService.importContainerManifest(containerManifestImportAllReqVO, ExImTypeEnum.IM.getValue());
        return CommonResult.success();
    }

    @OperateLog(type = OperateTypeEnum.IMPORT)
    @Operation(summary = "导入落驳集装箱清单-CNSHA")
    @PostMapping("/import/CNSHA")
    @PreAuthorize("@ss.hasPermission('doc:offloadinglist:import')")
    public CommonResult<Void> importContainerManifestCnsha(
        @Valid @RequestBody ContainerManifestImportCnshaAllReqVO containerManifestImportAllReqVO) {
        containerManifestService.importContainerManifestCnsha(containerManifestImportAllReqVO);
        return CommonResult.success();
    }

    @OperateLog(type = OperateTypeEnum.OTHER)
    @Operation(summary = "校验落驳集装箱清单")
    @PostMapping("/check/CNNGB")
    public CommonResult<List<ContainerManifestImportReqVO>> checkContainerManifestCnngb(
        @Valid @RequestBody ContainerManifestImportAllReqVO containerManifestImportAllReqVO) {
        return CommonResult.success(containerManifestService.checkContainerManifest(containerManifestImportAllReqVO,
            ExImTypeEnum.IM.getValue()));
    }

    @OperateLog(type = OperateTypeEnum.OTHER)
    @Operation(summary = "校验落驳集装箱清单")
    @PostMapping("/check/CNSHA")
    public CommonResult<List<ContainerManifestImportCnshaReqVO>> checkContainerManifestCnsha(
        @Valid @RequestBody ContainerManifestImportCnshaAllReqVO containerManifestImportCnshaAllReqVO) {
        return CommonResult
            .success(containerManifestService.checkContainerManifestCnsha(containerManifestImportCnshaAllReqVO));
    }

    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PostMapping("/excel-template/CNNGB")
    @Operation(summary = "下载CNNGB-excel导入模板")
    @PreAuthorize("@ss.hasPermission('doc:offloadinglist:download')")
    public CommonResult<Void> downloadExcelTemplateCnngb(HttpServletResponse response) {
        String fileName = "containerIMCNNGB.xlsx";
        String templatePath = "templates/containerIMCNNGB.xlsx";
        return FileOperateUtil.downloadExcelTemplate(response, fileName, templatePath);
    }

    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PostMapping("/excel-template/CNSHA")
    @Operation(summary = "下载CNSHA-excel导入模板")
    @PreAuthorize("@ss.hasPermission('doc:offloadinglist:download')")
    public CommonResult<Void> downloadExcelTemplateCnsha(HttpServletResponse response) {
        String fileName = "containerIMCNSHA.xls";
        String templatePath = "templates/containerIMCNSHA.xls";
        return FileOperateUtil.downloadExcelTemplate(response, fileName, templatePath);
    }

    @OperateLog(type = OperateTypeEnum.GET)
    @Operation(summary = "获得落驳集装箱清单分页")
    @PostMapping("/page")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:offloadinglist:search')")
    public CommonResult<PageResult<ContainerManifestRespVO>>
        getContainerManifestPage(@Valid @RequestBody ContainerManifestPageReqVO pageVo) {
        PageResult<ContainerManifestRespVO> pageResult =
            containerManifestService.getContainerManifestPage(pageVo, ExImTypeEnum.IM.getValue());
        return CommonResult.success(pageResult);
    }
}
